<?php

namespace App\Kuafu\WaiMai\Command;

use App\Kuafu\WaiMai\Model\WaiMaiOrderDetailModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class InitProductNameStandard extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'waimai:init_product_name_standard';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '外卖 - 初始化productNameStandard';

    /**
     * Create a new command instance.
     *
     * @return void//
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command
     *
     * @return
     */
    public function handle()
    {
        for ($i = 0; $i < 100; $i++) {

            while (true) {
                $row = WaiMaiOrderDetailModel::suffix($i)
                                             ->where('productName', '!=', '')
                                             ->where('productNameStandard', '')
                                             ->limit(10000)
                                             ->update(['productNameStandard' => DB::raw("REGEXP_REPLACE(productName, '\\\\[[^\\\\]]*\\\\]$', '')")]);

                $this->info("i:{$i} row:{$row} " . date('H:i:s'));

                if ($row == 0) {
                    break;
                }
            }
        }

    }
}
